import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyUtil {
    public static Product createProxy(Product p){
        Product product= (Product) Proxy.newProxyInstance(
                ProxyUtil.class.getClassLoader(),
                new Class[]{Product.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if("run".equals(method.getName())){
                            p.run();
                        }
                        else if("stop".equals(method.getName())){
                            p.stop();
                        }
                        return null;
                    }
                }
        );
        return product;
    }
}
